package com.darwin.darwinUnit
{
	import flash.utils.getQualifiedClassName;

	/**
	 * 测试一个方法
	 * @author lipi
	 */
	public class TestUtils_method
	{
		public function TestUtils_method(testCase:DarwinTestCase)
		{
			_case = testCase;
		}
		private var _case:DarwinTestCase;
		
		/**
		 * 方法所在的类例
		 */
		public var instance:Object;
		
		/**
		 * 要测试的方法
		 */
		public var methodName:String;
		
		/**
		 * 传入的参数
		 */
		public var methodArg:* = null;
		
		/**
		 * 应该返回的结果
		 */
		public var result:* = null;
		
		/**
		 * 外部校验函数,即回调函数所得到的值
		 */
		public var verifyFn:Function;
		
		/**
		 * 回调函数中验证的值
		 */
		public var verifyData:Object;
		
		
		public function run():*
		{
			if(methodArg != null)
			{
				if(!(methodArg is Array))
				{
					methodArg = [methodArg];
				}
			}
			var $result:Object;
			var $testResult:TestResult = new TestResult();
			$testResult.className = getQualifiedClassName(instance);
			$testResult.methodName = methodName;
			
			var $method:Function = instance[methodName];
			if($method == null)
			{
				$testResult.succeed = false;
				$testResult.type = TestResult.NOT_METHOD;
				$testResult.desc = "你要测试的方法不存在";
				$testResult.arg = methodArg;
				$testResult.result = "未执行";
				_case.pushResult($testResult);
				return "没正确执行";
			}else{
				$result = $method.apply(instance,methodArg);
				if($result != result)
				{
					$testResult.succeed = false;
					$testResult.type = TestResult.ERROR_RESULT;
					$testResult.desc = "返回的结果不正确";
					$testResult.arg = methodArg;
					$testResult.result = $result;
					_case.pushResult($testResult);
					return $result;
				}else{
					if(verifyFn == null)
					{
						$testResult.succeed = true;
						$testResult.type = TestResult.OK;
						$testResult.desc = "";
						$testResult.arg = methodArg;
						$testResult.result = $result;
						_case.pushResult($testResult);
						return $result;
					}else{
						var $data:Object = verifyFn.apply();
						if($data == TestResult.CALLBACK && verifyData != TestResult.CALLBACK)
						{
							$testResult.succeed = false;
							$testResult.type = TestResult.NOT_CALLBACK;
							$testResult.desc = "需要的回调未执行";
							$testResult.arg = methodArg;
							$testResult.result = $data;
							_case.pushResult($testResult);
						}else if(compare($data,verifyData) == false){
							$testResult.succeed = false;
							$testResult.type = TestResult.ERROR_CALLBACK;
							$testResult.desc = "回调的返回结果不正确";
							$testResult.arg = methodArg;
							$testResult.result = $data;
							_case.pushResult($testResult);
						}else{
							$testResult.succeed = true;
							$testResult.type = TestResult.OK;
							$testResult.desc = "";
							$testResult.arg = methodArg;
							$testResult.result = $result;
							_case.pushResult($testResult);
							return $result;
						}
						
						return $result;
					}
				}
			}
			return "未知";
		}
		
		private function compare(data1:Object,data2:Object):Boolean
		{
			if(data1 == data2)
			{
				return true;
			}else if((data1 is String || data2 is String) && (data1 != data2)){
				return false;
			}else{
				var $return:Boolean = true;
				var key:Object;
				for each(key in data1)
				{
					if(data1[key] != data2[key])
					{
						return false;
					}
				}
				for each(key in data2)
				{
					if(data1[key] != data2[key])
					{
						return false;
					}
				}
			}
			return true;
		}
		
		
	}
}